package com.central.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.central.common.page.PageData;
import com.central.common.utils.ConvertUtils;
import com.central.common.utils.Result;
import com.central.common.validator.ValidatorUtils;
import com.central.common.validator.group.AddGroup;
import com.central.common.validator.group.DefaultGroup;
import com.central.common.validator.group.UpdateGroup;
import com.central.modules.sys.dto.PageSysNoticeVo;
import com.central.modules.sys.entity.SysNoticeEntity;
import com.central.modules.sys.model.AddSysNoticeRequest;
import com.central.modules.sys.model.SysNoticeQueryBean;
import com.central.modules.sys.service.SysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@RequiredArgsConstructor
@RestController
@RequestMapping("/sys_notice")
public class SysNoticeController {
    private final SysNoticeService sysNoticeService;

    @GetMapping("/page_search")
    public Result<PageData<PageSysNoticeVo>> pageSearch(SysNoticeQueryBean queryBean) {
        IPage<SysNoticeEntity> page = sysNoticeService.searchPage(queryBean);
        return Result.succeed(PageData.copy(page, page.getRecords().stream().map(e -> {
            return ConvertUtils.sourceToTarget(e, PageSysNoticeVo.class);
        }).collect(Collectors.toList())));
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody AddSysNoticeRequest request) {
        //效验数据
        ValidatorUtils.validateEntity(request, AddGroup.class, DefaultGroup.class);
        sysNoticeService.add(request);
        return Result.succeed(null);
    }

    @PutMapping("/update")
    public Result<Void> update(@RequestBody AddSysNoticeRequest request) {
        //效验数据
        ValidatorUtils.validateEntity(request, UpdateGroup.class, DefaultGroup.class);
        sysNoticeService.updateById(request.getId(), request);
        return Result.succeed(null);
    }

    @DeleteMapping("/del")
    public Result<Void> del(@RequestParam Long id) {
        sysNoticeService.deleteById(id);
        return Result.succeed(null);
    }

}
